﻿/*******************************************************
 * 
 * 作者：罗敏贵
 * 说明：
 * 运行环境：.NET 4.0.0
 * 版本号：1.0.0
 * 
 * 历史记录：
 * 创建文件 罗敏贵 20140925
 * 
*******************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Caching;
using System.Text.RegularExpressions;

namespace 拼接字符串 {
    public partial class MainFrm: Form {
        public MainFrm() {
            InitializeComponent();
        }
        private void txtContent_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) {
            if(e.KeyCode == Keys.F1) {
                //复制：
                if(txtContent.SelectedText != "") Clipboard.SetDataObject(txtContent.SelectedText);
            } else if(e.KeyCode == Keys.F12) {

            } else if((!e.Alt && e.Control) && (e.KeyCode == Keys.C)) {

            }
        }
        /// <summary>
        /// 此私有方法从一段HTML文本中提取出一定字数的纯文本
        /// </summary>
        /// <param name="instr">HTML代码</param>
        /// <param name="withLink">是否要链接里面的字</param>
        /// <returns>纯文本</returns>
        private string getFirstNchar(string instr) {
            string m_outstr = instr.Clone() as string;
            m_outstr = new Regex(@"(?m)<script[^>]*>(\w|\W)*?</script[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(m_outstr, string.Empty);
            m_outstr = new Regex(@"(?m)<link[^>]*>(\w|\W)*?[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(m_outstr, string.Empty);
            m_outstr = new Regex(@"(?m)<style[^>]*>(\w|\W)*?</style[^>]*>", RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(m_outstr, string.Empty);
            m_outstr = new Regex(@"\bstyle\b\s*=\s*(?<key>['""])[\s\S]*?\k<key>", RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(m_outstr, string.Empty);

            m_outstr = new Regex(@"<iframe[\s\S]+</iframe *>", RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(m_outstr, string.Empty);
            m_outstr = new Regex(@"<frameset[\s\S]+</frameset  *>", RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(m_outstr, string.Empty);
            m_outstr = new Regex(@"-->", RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(m_outstr, string.Empty);
            m_outstr = new Regex(@"<!--.*", RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(m_outstr, string.Empty);
            m_outstr = new Regex(@"(&nbsp;)+").Replace(m_outstr, "").Replace("\r\n", string.Empty).Replace("\r\n\r\n", "\r\n").Replace("\r\n\r\n", "\r\n");
            m_outstr = new Regex(@"\s", RegexOptions.Multiline | RegexOptions.IgnoreCase).Replace(m_outstr, string.Empty);

            return m_outstr;
        }

        private void 拼接字符串ToolStripMenuItem_Click(object sender, EventArgs e) {
            try {
                #region 拼接字符串
                if(txtContent.Text.Trim() != "") {
                    StringBuilder l = new StringBuilder();
                    l.AppendLine("StringBuilder builder = new StringBuilder();");
                    string tempname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                        DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
                    string filename = System.Windows.Forms.Application.StartupPath + @"\\family_" + tempname + ".txt";
                    StreamWriter sw2 = new StreamWriter(filename, false, Encoding.UTF8);
                    sw2.Write(txtContent.Text);
                    sw2.Flush();
                    sw2.Close();
                    sw2.Dispose();
                    txtContent.Text = "";
                    System.IO.StreamReader read = new System.IO.StreamReader(filename);
                    while(true) {
                        string currentLine = read.ReadLine();
                        if(currentLine == null) {
                            break;
                        }
                        l.AppendLine("builder.AppendLine(\"" + currentLine.Replace("\"", "\\\"") + " \");");
                    }
                    read.Dispose();
                    File.Delete(filename);
                    txtContent.Text = l.ToString();
                }
                #endregion
            } catch(Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        private void 反拼接字符串ToolStripMenuItem_Click(object sender, EventArgs e) {
            try {
                #region 读取StringBuilder里的数据
                if(txtContent.Text.Trim() != "") {
                    StringBuilder lismsg = new StringBuilder();
                    string strContent = txtContent.Text;
                    string pattern = @"\(""(?<dd>.*?)""\)";
                    var reg = new Regex(pattern);
                    MatchCollection mc = reg.Matches(strContent);
                    foreach(Match m in mc) {
                        lismsg.AppendLine(m.Groups[1].Value);
                    }
                    txtContent.Text = lismsg.ToString();
                }
                #endregion
            } catch(Exception ex) {
                MessageBox.Show(ex.Message);

            }
        }

        private void switchCase转换ToolStripMenuItem_Click(object sender, EventArgs e) {
            try {
                #region 拼接字符串
                if(txtContent.Text.Trim() != "") {
                    StringBuilder l = new StringBuilder();
                    l.AppendLine("StringBuilder builder = new StringBuilder();");
                    string tempname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                        DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();

                    string filename = System.Windows.Forms.Application.StartupPath + @"\\family_" + tempname + ".txt";
                    StreamWriter sw2 = new StreamWriter(filename, false, Encoding.UTF8);
                    sw2.Write(txtContent.Text);
                    sw2.Flush();
                    sw2.Close();
                    sw2.Dispose();
                    txtContent.Text = "";
                    System.IO.StreamReader read = new System.IO.StreamReader(filename);

                    while(true) {
                        string currentLine = read.ReadLine();
                        if(currentLine == null) {
                            break;
                        }
                        l.AppendLine("case \"" + currentLine.Replace("\"", "\\\"") + " \":");
                    }
                    read.Dispose();
                    File.Delete(filename);
                    txtContent.Text = l.ToString();
                }
                #endregion
            } catch(Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        private void 属性生成器ToolStripMenuItem_Click(object sender, EventArgs e) {
            try {
                #region 拼接字符串
                if(txtContent.Text.Trim() != "") {
                    StringBuilder l = new StringBuilder();
                    string tempname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                        DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();

                    string filename = System.Windows.Forms.Application.StartupPath + @"\\family_" + tempname + ".txt";
                    StreamWriter sw2 = new StreamWriter(filename, false, Encoding.UTF8);
                    sw2.Write(txtContent.Text);
                    sw2.Flush();
                    sw2.Close();
                    sw2.Dispose();
                    txtContent.Text = "";
                    System.IO.StreamReader read = new System.IO.StreamReader(filename);

                    while(true) {
                        string currentLine = read.ReadLine();
                        if(currentLine == null) {
                            break;
                        }
                        l.AppendLine(" public string " + currentLine.Replace("\"", "\\\"") + " ;");
                    }
                    read.Dispose();
                    File.Delete(filename);
                    txtContent.Text = l.ToString();
                }
                #endregion
            } catch(Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }

        private void documentwriteToolStripMenuItem_Click(object sender, EventArgs e) {
            try {
                #region 拼接字符串
                if(txtContent.Text.Trim() != "") {
                    StringBuilder l = new StringBuilder();
                    string tempname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                        DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
                    string filename = Application.StartupPath + @"\\family_" + tempname + ".txt";
                    StreamWriter sw2 = new StreamWriter(filename, false, Encoding.UTF8);
                    sw2.Write(txtContent.Text);
                    sw2.Flush();
                    sw2.Close();
                    sw2.Dispose();
                    txtContent.Text = "";
                    StreamReader read = new StreamReader(filename);
                    while(true) {
                        string currentLine = read.ReadLine();
                        if(currentLine == null) {
                            break;
                        }
                        l.AppendLine("document.write(\"" + currentLine.Replace("\"", "'") + "\"); ");
                    }
                    read.Dispose();
                    File.Delete(filename);
                    txtContent.Text = l.ToString();
                }
                #endregion
            } catch {
                #region 拼接字符串
                if(txtContent.Text.Trim() != "") {
                    string temp = txtContent.Text
                        .Replace("\"", "'")
                        .Replace(" ", "")
                        .Replace("\n", "")
                        .Replace("\r", "")
                        .Replace("\r\n", "")
                        .Trim();
                    txtContent.Text = temp;
                }
                #endregion
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e) {
            //读取文本
            string instr = txtContent.Text.Trim();
            string content = getFirstNchar(instr);
            txtContent.Text = content;
        }
        int i = 0;
        private void toolStripButton2_Click(object sender, EventArgs e) {
           //大小写转换
            string temp = txtContent.Text;
            if(i == 0) {
                txtContent.Text = temp.ToUpper();
                i = 1;
            } else {
                txtContent.Text = temp.ToLower();
                i = 0;
            }
        }
        private void jS拼接ToolStripMenuItem_Click(object sender, EventArgs e) {
            try {
                #region 拼接字符串
                if(txtContent.Text.Trim() != "") {
                    StringBuilder l = new StringBuilder();
                    l.AppendLine("var _html='';");
                    string tempname = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() +
                        DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString();
                    string filename = System.Windows.Forms.Application.StartupPath + @"\\family_" + tempname + ".txt";
                    StreamWriter sw2 = new StreamWriter(filename, false, Encoding.UTF8);
                    sw2.Write(txtContent.Text);
                    sw2.Flush();
                    sw2.Close();
                    sw2.Dispose();
                    txtContent.Text = "";
                    System.IO.StreamReader read = new System.IO.StreamReader(filename);
                    while(true) {
                        string currentLine = read.ReadLine();
                        if(currentLine == null) {
                            break;
                        }
                        l.AppendLine(" _html+=\"" + currentLine.Replace("\"", "\\\"") + " \";");
                    }
                    read.Dispose();
                    File.Delete(filename);
                    txtContent.Text = l.ToString();
                }
                #endregion
            } catch(Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
